//
//  TabBarController.m
//  MingJiangApp
//
//  Created by wangliang on 15/2/24.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "TabBarController.h"
#import "UIImage+Addition.h"

#import "MyTabBar.h"


@interface TabBarController ()<MyTabBarDelegate>

@property (nonatomic, weak) UIButton *selectedBtn;
@property (nonatomic, strong) MyTabBar *myView;

@end

@implementation TabBarController

#pragma mark - VC自带方法
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self UI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark -设置UI界面
- (void)UI {
    
    CGRect rect = self.tabBar.frame;
    [self.tabBar removeFromSuperview];  //移除TabBarController自带的下部的条
    
    //测试添加自己的视图
    self.myView = [[MyTabBar alloc] init];
    self.myView.delegate = self;
    self.myView.frame = rect;
    self.myView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myView];
    
    
    
    
    NSArray *imageS = @[@"tabbar_0_0",@"tabbar_1_0",@"tabbar_2_0",@"tabbar_3_0"];
    NSArray *selectImageS = @[@"tabbar_0_1",@"tabbar_1_1",@"tabbar_2_1",@"tabbar_3_1"];
    //为控制器添加按钮
    for (int i = 0 ; i < imageS.count; i ++) { //根据有多少个子视图控制器来进行添加按钮

        NSString *imageName = imageS[i];
        NSString *imageNameSel = selectImageS[i];
        
        UIImage *image = [UIImage imageNamed:imageName];
        UIImage *imageSel = [UIImage imageNamed:imageNameSel];
        
        [self.myView addButtonWithImage:image selectedImage:imageSel];
    }
    
    
   

}

/**
 *  自定义TabBar的按钮点击事件
 */
- (void)clickBtn:(UIButton *)button {
    //1.先将之前选中的按钮设置为未选中
    self.selectedBtn.selected = NO;
    //2.再将当前按钮设置为选中
    button.selected = YES;
    //3.最后把当前按钮赋值为之前选中的按钮
    self.selectedBtn = button;
    
    //4.跳转到相应的视图控制器. (通过selectIndex参数来设置选中了那个控制器)
    self.selectedIndex = button.tag;
    
    
}

/**永远别忘记设置代理*/
- (void)tabBar:(MyTabBar *)tabBar selectedFrom:(NSInteger)from to:(NSInteger)to {
    self.selectedIndex = to;
}


- (void)setIndex:(NSInteger)index {
    
    _index = index;
    [self.myView setIndex:index];
}

- (void)setTabbarHidden:(BOOL)tabbarHidden {
    
    _tabbarHidden = tabbarHidden;
    if (tabbarHidden) {
        
        self.myView.hidden = YES;
    }else {
        
        self.myView.hidden = NO;
    }
}

@end
